<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;
class NewController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //浏览
    public function index()
    {
       $data = DB::table("new")->get();
       //view("分配模板","分配数据")
       return view('new.index', ['data' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //执行添加页面
    public function create()
    {
       return view("new.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //新闻添加
    public function store(Request $request)
    {
        //insertGetId 返回的是自增ID
        //$res = DB::table('new')->insert( ['title' => $title,"aid"=>$aid]);
        $time = $request->created_at = date("Y-m-d H:i:s");
        $res = DB::table('new')->insert( ['title' => $request->title,"aid"=> $request->aid,"created_at"=>$time]);
        if($res > 0){
            echo "添加成功";
        }else{
            echo "添加失败";
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return "浏览单个页面";
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
       //
       $id = $request->route("new");
       $data = DB::table("new")->where("id",$id)->get();
       return view('new.edit', ['data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id = $request->id;
        $aid = $request->aid;
        $title = $request->title;
        $res = DB::table('new')->where('id', $id)->update(['aid' => $aid,"title"=>$title]);
        if($res){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return "执行删除页面";
    }

    //删除操作
    public function delete(Request $request)
    {
        $id = $request->id;
        $res = DB::table('new')->where('id',$id)->delete();
        if($res){
            echo "删除成功";
        }else{
            echo "删除失败";
        }
    }
}
